package com.nerve.android.cloudoffice.app.activity;

import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.app.worker.LoginWorker;
import com.nerve.android.cloudoffice.common.Const;

/**
 * @project: Cloudoffice
 * @file: LoginActivity.java
 * @package: com.nerve.android.cloudoffice.app.activity
 * @description:
 *
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: Aug 15, 2013
 * @change log:
 */
@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_login)
public class LoginActivity extends NavigationActivity implements OnClickListener{
	
	private boolean isAutoLogin = true; //是否自动登录
	
	private Drawable autoDrawable;
	
	
	@ViewOnId(id=R.id.name)
	protected EditText nameET;
	@ViewOnId(id=R.id.password)
	protected EditText passwordET;
	@ViewOnId(id=R.id.autoLogin, clickListener="this")
	protected TextView autoTV;
	
	@ViewOnId(id=R.id.okBtn, clickListener="this")
	protected Button loginBtn;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			hideProgressDialog();
			switch (msg.what) {
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			case WORKER_OK:
				onLoginSuccess();
				break;
			}
		}
	};
	
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("员工登录");
		showNavigateButton(false, false);
		
		changeAutoDrawable();
	}
	
	@Override
	protected void initData() {
		SharedPreferences sp = getSharedPreferences(AutoLoginActivity.SHARED_FILE, MODE_PRIVATE);
		isAutoLogin = sp.getBoolean(AutoLoginActivity.AUTO, true);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//处理自动登录点击
		case R.id.autoLogin:
			isAutoLogin = !isAutoLogin;
			System.out.println("autoLogin click！！！" + isAutoLogin);
			changeAutoDrawable();
			break;
			
		case R.id.okBtn:
			loginDo();
			break;
		}
	}
	
	private void changeAutoDrawable(){
		if(isAutoLogin){
			if(autoDrawable == null){
				autoDrawable = getResources().getDrawable(R.drawable.ico_ok);
				autoDrawable.setBounds(0, 0, autoDrawable.getMinimumWidth(), autoDrawable.getMinimumHeight());
			}
			autoTV.setCompoundDrawables(null, null, autoDrawable, null);
		}else{
			autoTV.setCompoundDrawables(null, null, null, null);
		}
	}
	
	/**
	 * 去登录
	 */
	private void loginDo(){
		String name = nameET.getText().toString();
		String pass = passwordET.getText().toString();
		
		if(name.trim().length() == 0){
			toast("帐号不能为空");
			return ;
		}
		
		if(pass.trim().length() == 0){
			toast("密码不能为空");
			return ;
		}
		
		showProgressDialog(null, "正在登录，请稍候...");
		new LoginWorker(handler).execute(name, pass);
	}
	
	/**
	 * @method name: onLoginSuccess
	 * @description: 登录成功后，先判断是否自动登录，然后再跳转
	 * @return type: void
	 *
	 */
	private void onLoginSuccess(){
		SharedPreferences sp = getSharedPreferences(AutoLoginActivity.SHARED_FILE, MODE_PRIVATE);
		Editor editor = sp.edit();
		
		if(isAutoLogin){
			editor.putBoolean(AutoLoginActivity.AUTO, true);
			editor.putString(AutoLoginActivity.NAME, 
					nameET.getText().toString());
			editor.putString(AutoLoginActivity.PASSWORD, 
					passwordET.getText().toString());
			
			
			
			Const.log("自动登录信息保存成功！");
		}else{
			editor.putBoolean(AutoLoginActivity.AUTO, false);
		}
		
		editor.commit();
		toActivity(IndexGroupActivity.class, true);
		finish();
	}
}